<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Device
 * @package App\Models
 * @version December 25, 2017, 9:32 am UTC
 *
 * @property string name
 * @property string ip
 * @property integer type
 * @property integer status
 * @property integer park_code
 */
class Device extends Model
{
    use SoftDeletes;

    public $table = 'devices';


    protected $dates = ['deleted_at'];


    public $fillable = [
        'name',
        'ip',
        'type',
        'park_code',
        'channel_id',
        'brand',
        'spec',
        'device_id',
        'img',
        'status',
        'buy_time',
        'use_time'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name' => 'string',
        'ip' => 'string',
        'type' => 'integer',
        'status' => 'integer',
        'spec' => 'integer',
        'brand' => 'integer',
        'device_id' => 'string'
    ];

    public function getImgAttribute($value)
    {
        if (!$value) {
            return [];
        }

        $value = collect(explode(',', $value));

        $newValue = $value->map(function ($item) {
            if ($item) {
                return [
                    'name' => $item,
                    'url' => config('custom.file_visit_url') . '/' . $item
                ];
            }
        });

        return $newValue;
    }
}
